package com.hb.trans.mvp.ui.activity


import android.annotation.SuppressLint
import android.os.Bundle
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import android.view.MenuItem
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.basiclib.base.BaseActivity
import com.hb.trans.R
import com.hb.trans.bean.SearchItem
import com.hb.trans.global.ACTIVITY_COMPANY_SEARCH
import com.hb.trans.mvp.presenter.NewDetailPresenter
import com.hb.trans.mvp.ui.adapter.CompanySearch1Adapter
import com.hb.trans.mvp.ui.adapter.CompanySearch2Adapter
import com.jakewharton.rxbinding2.view.clicks
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_company_search.*
import javax.inject.Inject


@Route(path = ACTIVITY_COMPANY_SEARCH)
class CompanySearchActivity : BaseActivity() {
    @Inject
    lateinit var mAdapter1: CompanySearch1Adapter
    @Inject
    lateinit var mAdapter3: CompanySearch1Adapter
    @Inject
    lateinit var mAdapter4: CompanySearch1Adapter
    @Inject
    lateinit var mAdapter2: CompanySearch2Adapter

    private val menu1Data = ArrayList<SearchItem>()
    private val menu2Data = ArrayList<SearchItem>()
    private val menu3Data = ArrayList<SearchItem>()
    private val menu4Data = ArrayList<SearchItem>()

    var show1 = false
    var show2 = false
    var show3 = false


    @Inject
    override fun getContentViewResId(): Int = R.layout.activity_company_search

    val s1: List<String> by lazy { resources.getStringArray(R.array.car_type).toList() }
    val s2: List<String> by lazy { resources.getStringArray(R.array.trans_type).toList() }


    override fun onActivityInject() {
        AndroidInjection.inject(this)
    }

    override fun attachView(savedInstanceState: Bundle?) {
        ARouter.getInstance().inject(this)
    }

    override fun getPresenter(): NewDetailPresenter? = null

    override fun initView() {
        setSupportActionBar(companySearchToolbar.toolbar)
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        supportActionBar!!.setHomeAsUpIndicator(R.mipmap.ic_left_arrow)
        initMenus()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> onBackPressed()
        }
        return true
    }

    override fun initData() {
    }

    @SuppressLint("CheckResult")
    override fun setListener() {
        mAdapter1.setOnItemClickListener { adapter, _, position ->
            menu1Data[position].chosen = !menu1Data[position].chosen
            mAdapter1.notifyDataSetChanged()
        }

        menuTitle1.clicks().subscribe {
            show1 = !show1
            setMenus()
        }
        menuTitle2.clicks().subscribe {
            show2 = !show2
            setMenus()
        }
        menuTitle3.clicks().subscribe {
            show3 = !show3
            setMenus()
        }
    }

    private fun setMenus(){
        if(show1){
            menu1.visibility = View.VISIBLE
            menu2.visibility = View.VISIBLE
        }else{
            menu1.visibility = View.GONE
            menu2.visibility = View.GONE
        }
        if(show2){
            menu3.visibility = View.VISIBLE
        }else{
            menu3.visibility = View.GONE
        }
        if(show3){
            menu4.visibility = View.VISIBLE
        }else{
            menu4.visibility = View.GONE
        }
    }
    private fun initMenusData(){
        for (index in 0..s1.size-1){
            var menu1Item = SearchItem(s1[index],false)
            menu1Data.add(menu1Item)
        }
        for (index in 0..s1.size-1){
            var menu2Item = SearchItem(s1[index],false)
            menu2Data.add(menu2Item)
        }
        for (index in 0..s1.size-1){
            var menu3Item = SearchItem(s1[index],false)
            menu3Data.add(menu3Item)
        }
        for (index in 0..s1.size-1){
            var menu4Item = SearchItem(s1[index],false)
            menu4Data.add(menu4Item)
        }
    }
    private fun initMenus(){
        initMenusData()
        menu1.isNestedScrollingEnabled = false
        menu2.isNestedScrollingEnabled = false
        menu3.isNestedScrollingEnabled = false
        menu4.isNestedScrollingEnabled = false

        val manager1 = androidx.recyclerview.widget.StaggeredGridLayoutManager(
            4,
            androidx.recyclerview.widget.StaggeredGridLayoutManager.VERTICAL
        )
        val manager3 = androidx.recyclerview.widget.StaggeredGridLayoutManager(
            4,
            androidx.recyclerview.widget.StaggeredGridLayoutManager.VERTICAL
        )
        val manager4 = androidx.recyclerview.widget.StaggeredGridLayoutManager(
            4,
            androidx.recyclerview.widget.StaggeredGridLayoutManager.VERTICAL
        )

        menu1.setLayoutManager(manager1)
        menu3.setLayoutManager(manager3)
        menu4.setLayoutManager(manager4)
        mAdapter1.setNewData(menu1Data)
        mAdapter2.setNewData(menu2Data)
        mAdapter3.setNewData(menu3Data)
        mAdapter4.setNewData(menu4Data)
        menu1.apply {
            adapter = mAdapter1
        }
        menu2.apply {
            adapter = mAdapter2
        }
        menu3.apply {
            adapter = mAdapter3
        }
        menu4.apply {
            adapter = mAdapter4
        }
    }
}
